package com.jiangyg.mall.coupon.controller;

import com.jiangyg.mall.core.support.restful.Pagination;
import com.jiangyg.mall.core.support.restful.Result;
import com.jiangyg.mall.coupon.dto.SeckillSkuDTO;
import com.jiangyg.mall.coupon.service.SeckillSkuService;
import com.jiangyg.mall.coupon.vo.SeckillSkuVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 类描述：秒杀活动商品关联控制器
 *
 * @author jiangyg
 * @version email@jiangyg.com
 * @date 2021-10-18 21:33:05
 */
@RestController
@RequestMapping("/coupon/seckill/sku/relation")
public class SeckillSkuController {

    /**
     * 秒杀活动商品关联接口
     */
    private final SeckillSkuService seckillSkuDao;

    @Autowired
    public SeckillSkuController(SeckillSkuService seckillSkuDao) {
        this.seckillSkuDao = seckillSkuDao;
    }

    /**
     * 功能描述：秒杀活动商品关联分页查询
     *
     * @param request    请求
     * @param pagination 分页参数
     * @return 返回分页秒杀活动商品关联
     */
    @ResponseBody
    @PostMapping("/select")
    public Result<Pagination<SeckillSkuVO>> select(HttpServletRequest request, @RequestBody Pagination<SeckillSkuVO> pagination) throws Exception {
        return Result.ok(seckillSkuDao.selectPagination(pagination));
    }

    /**
     * 功能描述：保存或者修改秒杀活动商品关联
     *
     * @param request 请求
     * @param dto     秒杀活动商品关联
     * @return 结果
     */
    @ResponseBody
    @PostMapping("/save")
    public Result<Object> save(HttpServletRequest request, @RequestBody @Validated SeckillSkuDTO dto) throws Exception {
        return Result.saveOrUpdate(seckillSkuDao.saveOrUpdate(dto));
    }

    /**
     * 功能描述：根据ID获取秒杀活动商品关联
     *
     * @param request 请求
     * @param seckillSkuRelationId  秒杀活动商品关联ID
     * @return 结果
     */
    @ResponseBody
    @PostMapping("/info")
    public Result<SeckillSkuVO> info(HttpServletRequest request, @RequestParam(name = "seckillSkuRelationId") Long seckillSkuRelationId) throws Exception {
        return Result.ok(seckillSkuDao.selectById(seckillSkuRelationId));
    }

    /**
     * 功能描述：删除秒杀活动商品关联
     *
     * @param request 请求
     * @param seckillSkuRelationIds 批量删除的秒杀活动商品关联ID
     * @return 结果
     */
    @ResponseBody
    @PostMapping("/delete")
    public Result<Object> delete(HttpServletRequest request, @RequestBody List<Long> seckillSkuRelationIds) {
        return Result.delete(seckillSkuDao.delete(seckillSkuRelationIds));
    }

}
